import React, { Component } from 'react'
import { creatIncrementAction, creatDecrementAction, creatIncrementAsyncAction } from "../../redux/count_action"
import { connect } from "react-redux"
// import { number } from "prop-types"

// import { INCREMNET, DECREMNET } from '../../redux/count_map.js'
class Count extends Component {

    increment = () => {
        const {value} = this.selectNumber
        this.props.jia(value*1)
    }

    decrement = () => {
        const {value} = this.selectNumber
        this.props.jian(value*1)
    }

    incrementIfOdd = () => {
        const {value} = this.selectNumber
        if (this.props.count % 2 !== 0) {
            console.log('222');
            this.props.jia(value*1)
        }
    }

    incrementAsync = () => {
        const {value} = this.selectNumber
        this.props.jiaAsync(value*1, 500)
    }

    render() {
        return (
            <div>
                <h1>当前求和为：{this.props.count}</h1>
                <select ref={c => this.selectNumber = c}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>
                <button onClick={this.increment}>+</button>
                <button onClick={this.decrement}>-</button>
                <button onClick={this.incrementIfOdd}>当前求和为奇数再加</button>
                <button onClick={this.incrementAsync}>异步加</button>
            </div>
        )
    }
}

// a函数返回的对象中的key就作为传递给UI组件props的key，value就作为传递给UI组件props的value----状态
// const mapStateToProps = state => ({count: state})

// b函数返回的对象中的key就作为传递给UI组件props的key，value就作为传递给UI组件props的value----操作状态的方法
// const mapDispatchToProps = dispatch => (
//     {
//         jia: number => dispatch(creatIncrementAction(number)),
//         jian: number => dispatch(creatDecrementAction(number)),
//         jiaAsync: (number, time) => dispatch(creatIncrementAsyncAction(number, time))
//     }
// )

// const CountContainer = connect(mapStateToProps, mapDispatchToProps)(CountUI)

export default connect(
    state => ({count: state}),
    // mapDispatchToProps的一般写法
    // dispatch => ({
    //     jia: number => dispatch(creatIncrementAction(number)),
    //     jian: number => dispatch(creatDecrementAction(number)),
    //     jiaAsync: (number, time) => dispatch(creatIncrementAsyncAction(number, time))
    // })

    // mapDispatchToProps的简写
    {
        jia: creatIncrementAction,
        jian: creatDecrementAction,
        jiaAsync: creatIncrementAsyncAction,
    }
)(Count)